package com.jhd.fuzhou.view.mail;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.service.LoginService;
import com.jhd.fuzhou.util.CustomToast;
import com.jhd.fuzhou.util.UtilHelper;
import com.jhd.fuzhou.vo.DeptVo;
import com.jhd.fuzhou.vo.JsonForResult;
import com.jhd.fuzhou.vo.OrganizationVo;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.StringUtil;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/*根据orgId获取deptId的list*/
public class MailListOrgActivity extends BaseActivity {
    @BindView(R.id.rv_org)RecyclerView rv_org;
    private Context context=this;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_mail_list_org);
    }

    @Override
    protected void processLogic() {
        onBack();
        LinearLayoutManager manager=new LinearLayoutManager(context);
        rv_org.setLayoutManager(manager);
        getOrgSelector();
    }

    private void getOrgSelector(){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        LoginService vs = retrofit.create(LoginService.class);
        Call<JsonForResult> jsonForResultCall = vs.getOrgSelector(application.getAccessToken());//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        List<OrganizationVo> organizationVoList= JSON.parseArray(jsonForResult.getData().toString(), OrganizationVo.class);
                        if(organizationVoList!=null){
                            for (OrganizationVo organizationVo : organizationVoList) {
                                OrgAdapter adapter=new OrgAdapter(organizationVoList);
                                rv_org.setAdapter(adapter);
                            }
                        }
                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络错误!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg + t.getMessage();
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }
    private void getDeptTree(OrganizationVo organizationVo,RecyclerView recyclerView,TextView textView,LinearLayout linearLayout){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
//        Retrofit retrofit = UtilHelper.retrofit(application, "http://192.168.1.192:9003/");//URL的头
        LoginService vs = retrofit.create(LoginService.class);
        Map<String, String> params = ParamsMapUtil.getInstance("orgId",organizationVo.getId());
        Call<JsonForResult> jsonForResultCall = vs.getDeptTree(application.getAccessToken(),params);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        if(jsonForResult.getData()!=null){
                            List<DeptVo> deptVoList = JSON.parseArray(jsonForResult.getData().toString(), DeptVo.class);
                            if(deptVoList!=null){
                                LinearLayoutManager manager=new LinearLayoutManager(context);
                                recyclerView.setLayoutManager(manager);
                                int num=0;
                                for (DeptVo deptVo : deptVoList) {
                                    num=num+deptVo.getPersonNum();
                                }
                                if(num>0){
                                    textView.setText(textView.getText().toString()+" ("+num+")");
                                    DeptAdapter adapter=new DeptAdapter(deptVoList,recyclerView);
                                    recyclerView.setAdapter(adapter);
                                }else{
                                    linearLayout.setVisibility(View.GONE);
                                }
                            }
                        }
                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络错误!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg + t.getMessage();
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }
    public class OrgAdapter extends RecyclerView.Adapter<OrgAdapter.ViewHolder> {
        private List<OrganizationVo> organizationVoList;

        public OrgAdapter(List<OrganizationVo> organizationVoList) {
            this.organizationVoList = organizationVoList;
        }

        @NonNull
        @Override
        public OrgAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mail_list_org, parent, false);
            OrgAdapter.ViewHolder viewHolder = new OrgAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull OrgAdapter.ViewHolder holder, int position) {
            holder.tv_organization.setText(organizationVoList.get(position).getName());
            getDeptTree(organizationVoList.get(position),holder.rv_mail_list,holder.tv_organization,holder.linearLayout);
            holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context,MailListDeptActivity.class);
                    intent.putExtra("orgId",organizationVoList.get(position).getId());
                    intent.putExtra("name",organizationVoList.get(position).getName());
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return organizationVoList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_organization;RecyclerView rv_mail_list;LinearLayout linearLayout;
            RelativeLayout relativeLayout;
            public ViewHolder(View itemView) {
                super(itemView);
                this.tv_organization = itemView.findViewById(R.id.tv_organization);
                this.rv_mail_list = itemView.findViewById(R.id.rv_mail_list);
                this.linearLayout = itemView.findViewById(R.id.linearLayout);
                this.relativeLayout = itemView.findViewById(R.id.relativeLayout);
            }
        }
    }
    public class DeptAdapter extends RecyclerView.Adapter<DeptAdapter.ViewHolder> {
        private List<DeptVo> deptVoList;private RecyclerView recyclerView;

        public DeptAdapter(List<DeptVo> deptVoList,RecyclerView recyclerView) {
            this.deptVoList = deptVoList;
            this.recyclerView = recyclerView;
        }

        @NonNull
        @Override
        public DeptAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mail_list_dept, parent, false);
            DeptAdapter.ViewHolder viewHolder = new DeptAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull DeptAdapter.ViewHolder holder, int position) {
            holder.tv_dept.setText(deptVoList.get(position).getName()+"("+(deptVoList.get(position).getPersonNum())+")");
            holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context,MailListDeptActivity.class);
                    intent.putExtra("deptId",deptVoList.get(position).getId());
                    intent.putExtra("name",deptVoList.get(position).getName());
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return deptVoList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_dept;LinearLayout linearLayout;
            public ViewHolder(View itemView) {
                super(itemView);
                this.tv_dept = itemView.findViewById(R.id.tv_dept);
                this.linearLayout = itemView.findViewById(R.id.linearLayout);
            }
        }
    }
}